/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMTermClassSettingsDialog;

import javax.swing.*;

import GuiTools.*;
import Settings.Language;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * @author Christoph Schnack
 *
 */
public class TCRangeDialog extends JDialog{
	JDialog dialog;
	DefaultListModel model;
	Vector<String> range;
	Language LANG;
	boolean isCanceled = true;
	
	public TCRangeDialog(Language lang, TribuitFrame parentFrame, DefaultComboBoxModel arg){
		super();
		dialog = this;
		LANG = lang;
		model = new DefaultListModel();
		for(int i = 0;i<arg.getSize();i++){
			model.addElement((String)arg.getElementAt(i));
		}
		final JList list = new JList(model);
		dialog.setTitle(LANG.getRange());
		dialog.setModal(true);
		JScrollPane jsp = new JScrollPane(list);
		jsp.setPreferredSize(new Dimension(300,300));
		JButton upBtn = new JButton("up");
		upBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String selection = (String) list.getSelectedValue();
				if(selection!=null){
					if(list.getSelectedIndex()>0){
						String old = (String)model.getElementAt(list.getSelectedIndex()-1);
						model.set(list.getSelectedIndex(), old);
						model.set(list.getSelectedIndex()-1, selection);
						list.setSelectedIndex(list.getSelectedIndex()-1);
					}
				}
			}
		});
		JButton downBtn = new JButton("down");
		downBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String selection = (String) list.getSelectedValue();
				if(selection!=null){
					if(list.getSelectedIndex()<model.getSize()-1){
						String old = (String)model.getElementAt(list.getSelectedIndex()+1);
						model.set(list.getSelectedIndex(), old);
						model.set(list.getSelectedIndex()+1, selection);
						list.setSelectedIndex(list.getSelectedIndex()+1);
					}
				}
			}
		});
		JPanel listButtonPanel = new JPanel();
		listButtonPanel.add(upBtn);
		listButtonPanel.add(downBtn);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
		centerPanel.add(jsp);
		centerPanel.add(listButtonPanel);
		JButton okBtn = new JButton("ok"), cancelBtn = new JButton(LANG.getCancel());
		okBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				range = new Vector<String>();
				for(int i=0;i<model.size();i++){
					range.add((String)model.elementAt(i));
				}
				isCanceled = false;
				dialog.dispose();
			}
		});
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialog.dispose();
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);
		dialog.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getRange()));
		dialog.getContentPane().add(BorderLayout.CENTER,centerPanel);
		dialog.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		dialog.pack();
		dialog.setLocationRelativeTo(parentFrame);
		dialog.setVisible(true);
	}
	
	public Vector<String> getNewRange(){
		return range;
	}
	
	public boolean isCanceled(){
		return isCanceled;
	}
}
